package com.bzsg.rightsManagementSystem.controller

import com.alibaba.fastjson.JSON
import com.bzsg.rightsManagementSystem.pojo.Role
import com.bzsg.rightsManagementSystem.service.PermissionService
import com.bzsg.rightsManagementSystem.service.RoleService
import com.bzsg.rightsManagementSystem.util.result.ErrorType
import com.bzsg.rightsManagementSystem.util.result.RequestResult
import com.bzsg.rightsManagementSystem.util.result.ResultUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*

@Controller
class RoleController {
    @Autowired
    private lateinit var roleService: RoleService

    @Autowired
    private lateinit var permissionService: PermissionService

    @PostMapping(value = ["api/addRole"])
    @ResponseBody
    fun addRole(@RequestBody role: Role): RequestResult<*> {
        return try {
            roleService.insert(role)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/deleteRoleById"])
    @ResponseBody
    fun deleteRole(@RequestParam roleId: Int): RequestResult<*> {
        return try {
            roleService.deleteById(roleId)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/updateRole"])
    @ResponseBody
    fun updateRole(@RequestBody role: Role): RequestResult<*> {
        return try {
            roleService.update(role)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/queryRole"])
    @ResponseBody
    fun queryRole(): RequestResult<*> {
        return try {
            ResultUtil.success(roleService.queryAll())
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/selectTotalRole"])
    @ResponseBody
    fun selectTotalRole(): RequestResult<*> {
        return try {
            ResultUtil.success(roleService.selectTotalRole())
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/queryAllRoleByLimit"])
    @ResponseBody
    fun queryAllByLimit(@RequestParam pageNow: Int, @RequestParam pageSize: Int): RequestResult<*> {
        return try {
            ResultUtil.success(roleService.queryAllByLimit(pageNow, pageSize))
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/getPermissionsByRole"])
    @ResponseBody
    fun getPermissionByRole(@RequestParam roleId: Int): RequestResult<*> {
        return try {
            val permissions = permissionService.selectPermissionsByRoleId(roleId)
            ResultUtil.success(permissions)
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/selectPermissionsByRoleId"])
    @ResponseBody
    fun selectPermissionsByRoleId(@RequestParam roleId: Int): RequestResult<*> {
        return try {
            val permissions = permissionService.selectPermissionsByRoleId(roleId)
            ResultUtil.success(permissions)
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/updateRolePermissionRelation"])
    @ResponseBody
    fun updateRolePermissionRelation(
        @RequestBody permissionIds: String?,
        @RequestParam roleId: Int
    ): RequestResult<*> {
        return try {
            val json = JSON.parseObject(permissionIds)
            val list = json["permissionIds"] as List<Int>
            if (list.isNotEmpty()) {
                roleService.deleteAllRolePermissionRelationByRoleId(roleId)
                for (permissionId in list) {
                    roleService.addRolePermissionRelation(roleId, permissionId)
                }
            } else {
                return ResultUtil.error(ErrorType.UNKNOWN)
            }
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }
}